package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.GoodsDao;
import com.aaa.entity.Goods;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {

    BaseDao baseDao = BaseDao.getInstance();

    @Override
    public int addGoods(Goods goods) {

        String sql = "insert into goods (name,code,type,unitId,price,categoryId) values(?,?,?,?,?,?)";

        Object[] objects = {goods.getName(),goods.getCode(),goods.getType(),goods.getCategoryId(),goods.getPrice(),goods.getCategoryId()};

        return  baseDao.executeUpdate(sql,objects);

    }

    @Override
    public  List<Goods> consumerGoodList(Integer pageNumber, Integer pageSize, String searchGoods, String searchName) throws Exception {
        String sql = "select " +
                " g.*,ca.name categoryName,un.name unitName " +
                " from " +
                " goods g LEFT JOIN category ca ON g.categoryId=ca.id LEFT JOIN unit un on g.unitId=un.id " +
                "where g.status = 1 ";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and g.goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            sql += " and g.name like '%" + searchName+"%'";
        }
        sql += " order by g.goodsId desc limit ?,?";
        Object[] params = {pageNumber, pageSize};
        List<Goods> list = baseDao.query(sql, params, Goods.class);
        return list;
    }

    @Override
    public int getConsumerGoodListCount(String searchGoods, String searchName) {
        String sql = "select count(1) len from goods where status = 1";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            sql += " and name like '%" + searchName+"%'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

    @Override
    public int consumerGoods(Integer goodsId, Integer code) {
        /**
         * 1.查出当前商品号库存量
         * 2.减去购买的库存量
         * 3.将减去购买的数量后的库存量重新放入数据库中
         */

        String sql1 = "update goods set code = code -? where id = ?";
        Object[] obj= {code,goodsId};
        int res = baseDao.executeUpdate(sql1,obj);
        return res;
    }

    @Override
    public List<Goods> getConsumerGoodsList(Integer pageNumber, Integer pageSize, String searchGoods, String searchName) {
        String sql = "select " +
                " g.*,ca.name categoryName,un.name unitName " +
                " from " +
                " goods g LEFT JOIN category ca ON g.categoryId=ca.id LEFT JOIN unit un on g.unitId=un.id " +
                "where g.status = 1 ";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and g.goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            sql += " and g.name like '%" + searchName+"%'";
        }
        sql += " order by g.goodsId desc limit ?,?";
        Object[] params = {pageNumber, pageSize};
        List<Goods> list = baseDao.query(sql, params, Goods.class);
        return list;
    }

    @Override
    public int getConsumerGoodsInfoCount(String searchGoods, String searchName) {
        String sql = "select count(1) len from goods where status = 1";
        if (StringUtils.isNotBlank(searchGoods)) {
            sql += " and goodsId = " + searchGoods;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and name like '" + searchName + "'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }

}
